#include "SqlMgr.h"


CSqlMgr::CSqlMgr()
{
    m_pMySql = NULL;
}


CSqlMgr::~CSqlMgr()
{
    if (m_pMySql != NULL)
    {
        mysql_close(m_pMySql);
        m_pMySql = NULL;
    }
}

bool CSqlMgr::Init(const string stUser, const string stPassword, const string stIp, const int iPort, const string stDatabase)
{
    MYSQL *pNewMySql = mysql_init(NULL);

    if (pNewMySql == NULL)
    {
        return false;
    }

    pNewMySql = mysql_real_connect(pNewMySql, stIp.c_str(), stUser.c_str(), stPassword.c_str(), stDatabase.c_str(), iPort, NULL, 0);
    if (pNewMySql == NULL)
    {
        return false;
    }

    if (m_pMySql != NULL)
    {
        mysql_close(m_pMySql);
    }

    m_pMySql = pNewMySql;
    return true;
}

bool CSqlMgr::Select(const string stTabel, vector<string> vecField, map<string, string> &mapWhere, vector<vector<string> >&vecVecResult)
{
    if (m_pMySql == NULL)
    {
        return false;
    }

    string sSql = "select ";

    if (vecField.empty())
    {
        sSql.append("*");
    }
    else
    {
        int iFieldSize = vecField.size();
        for (int iField = 0; iField < iFieldSize; ++iField)
        {
            sSql.append(vecField[iField]);

            if (iField != iFieldSize - 1)
            {
                sSql.append(" , ");
            }
        }
    }

    sSql.append(" from ");

    sSql.append(stTabel);

    if (!mapWhere.empty())
    {
        sSql.append(" where ");

        map<string, string>::const_iterator mapIt = mapWhere.begin();
        int iMapSize = mapWhere.size();
        for (int iIndex = 0; mapIt != mapWhere.end(); ++mapIt, ++iIndex)
        {
            sSql.append(mapIt->first);
            sSql.append(" = ");
            sSql.append(mapIt->second);

            if (iIndex != iMapSize - 1)
            {
                sSql.append(" and ");
            }
        }
    }

    int iRet = mysql_real_query(m_pMySql, sSql.c_str(), sSql.size());
    if (iRet != 0)
    {
        return false;
    }

    MYSQL_RES *pMySqlRes = mysql_store_result(m_pMySql);
    if (pMySqlRes == NULL)
    {
        return false;
    }

    string sField = "";
    while (MYSQL_ROW MySqlRow = mysql_fetch_row(pMySqlRes))
    {
        int iColSize = mysql_num_fields(pMySqlRes);
        unsigned long *pFieldsLength = mysql_fetch_lengths(pMySqlRes);
        vector<string> vecRow;
        for (int iCol = 0; iCol < iColSize; ++iCol)
        {
            sField.assign(MySqlRow[iCol], pFieldsLength[iCol]);
            vecRow.push_back(sField);
        }
        vecVecResult.push_back(vecRow);
    }

    mysql_free_result(pMySqlRes);

    return true;
}

bool CSqlMgr::Insert(const string stTabel, map<string, string> &mapKeyValue)
{

    return true;
}

bool CSqlMgr::Update(const string stTabel, const string stKey, const string stValue, map<string, string> &mapKeyValue)
{

    return true;
}
